package com.person.jiaiplan.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.person.jiaiplan.assembler.CalendarAssembler;
import com.person.jiaiplan.pojo.R;
import com.person.jiaiplan.pojo.dto.calendar.EveryDayWriteQueryDTO;
import com.person.jiaiplan.pojo.entity.EverydayWriteLog;
import com.person.jiaiplan.pojo.signle.SignleSnowFlake;
import com.person.jiaiplan.service.EverydayWriteLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 每日计划写入接口
 */
@AllArgsConstructor
@RestController
@RequestMapping("everyDay/write/")
@Validated
@Slf4j
public class EveryDayWriteController {

    @Autowired
    private EverydayWriteLogService everydayWriteLogService;


    /**
     * 插入
     *
     * @param
     * @return
     */
    @PostMapping("insert")
    public R<Boolean> insertOrUpdate(@RequestBody EveryDayWriteQueryDTO reqDTO) {
        if (reqDTO.getContentType() == 1) {
            Date saveTime = DateUtil.beginOfDay(reqDTO.getSaveTime());
            EverydayWriteLog everydayWriteLog = everydayWriteLogService.getOne(Wrappers.<EverydayWriteLog>lambdaQuery().eq(EverydayWriteLog::getSaveTime, saveTime));
            if (everydayWriteLog != null) {

                everydayWriteLog.setSaveTime(saveTime);
                everydayWriteLog.setContentSummary(reqDTO.getContentSummary());
                everydayWriteLog.setRemainingProblems(reqDTO.getRemainingProblems());
                // everydayWriteLog.setTemporaryRecord(reqDTO.getTemporaryRecord());
                everydayWriteLog.setCreateTime(new Date());
                everydayWriteLog.setContentType(1);
                return R.ok(everydayWriteLogService.updateById(everydayWriteLog));

            } else {
                everydayWriteLog = new EverydayWriteLog();
                everydayWriteLog.setId(SignleSnowFlake.getSnowflakeId());
                everydayWriteLog.setSaveTime(saveTime);
                everydayWriteLog.setContentSummary(reqDTO.getContentSummary());
                everydayWriteLog.setRemainingProblems(reqDTO.getRemainingProblems());
                everydayWriteLog.setCreateTime(new Date());
                everydayWriteLog.setContentType(1);
                return R.ok(everydayWriteLogService.save(everydayWriteLog));
            }

        }
        // 插入问题总览
        else if (reqDTO.getContentType() == 2) {
            EverydayWriteLog everydayWriteLog = everydayWriteLogService.getOne(Wrappers.<EverydayWriteLog>lambdaQuery().eq(EverydayWriteLog::getContentType, 2));
            if (everydayWriteLog != null) {
                everydayWriteLog.setRemainingProblems(reqDTO.getRemainingProblems());
                everydayWriteLog.setCreateTime(new Date());
                everydayWriteLog.setContentType(2);
                return R.ok(everydayWriteLogService.updateById(everydayWriteLog));
            } else {
                everydayWriteLog = new EverydayWriteLog();
                everydayWriteLog.setId(SignleSnowFlake.getSnowflakeId());
                everydayWriteLog.setRemainingProblems(reqDTO.getRemainingProblems());
                everydayWriteLog.setCreateTime(new Date());
                everydayWriteLog.setContentType(2);
                return R.ok(everydayWriteLogService.save(everydayWriteLog));
            }

        }
        return R.ok(false);

    }


    /**
     * 查询
     *
     * @param
     * @return
     */
    @GetMapping("query")
    public R<EveryDayWriteQueryDTO> query() {
        Date saveTime = DateUtil.beginOfDay(new Date());
        EverydayWriteLog everydayWriteLog = everydayWriteLogService.getOne(Wrappers.<EverydayWriteLog>lambdaQuery().eq(EverydayWriteLog::getSaveTime, saveTime));
        EverydayWriteLog problems = everydayWriteLogService.getOne(Wrappers.<EverydayWriteLog>lambdaQuery().eq(EverydayWriteLog::getContentType, 2));
        if (everydayWriteLog == null){
            everydayWriteLog = new EverydayWriteLog();
            everydayWriteLog.setId(SignleSnowFlake.getSnowflakeId());
            everydayWriteLog.setSaveTime(saveTime);
            everydayWriteLog.setCreateTime(new Date());
            everydayWriteLog.setContentType(1);
            if (problems != null){
                everydayWriteLog.setRemainingProblems(problems.getRemainingProblems());
            }
            everydayWriteLogService.save(everydayWriteLog);
        }
        if (everydayWriteLog != null && problems != null) {
            problems.setRemainingProblems(problems.getRemainingProblems());
        }
        return R.ok(CalendarAssembler.INSTANCE.toEveryDayWriteQueryDTO(everydayWriteLog));
    }
}
